﻿using System;
using System.Data;
using System.Data.SqlClient;
using Blog.Common.Helpers;
using Common.Constant;
using Entity;
using log4net;

namespace DataAccess
{
    public class CommentDao
    {
        private static readonly ILog Logger = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
        private SqlConnection _sqlCon;
        private DataSet _dataSet;

        public int AddComment(Comment comment)
        {
            try
            {
                var sqlParametters = new SqlParameter[6];
                sqlParametters[0] = new SqlParameter("@CommentContent", DbType.String) { Value = comment.CommentContent };
                sqlParametters[1] = new SqlParameter("@CommentDate", DbType.String) { Value = comment.CommentDate };
                sqlParametters[2] = new SqlParameter("@PostId", DbType.String) { Value = comment.PostId };
                sqlParametters[3] = new SqlParameter("@IsPublish", DbType.DateTime) { Value = comment.IsPublish };
                sqlParametters[4] = new SqlParameter("@UserName", DbType.String) { Value = comment.UserName };
                sqlParametters[5] = new SqlParameter("@Email", DbType.String) { Value = comment.Email };
                _sqlCon = DBConnection.SqlGetConnection();
                return SqlHelper.ExecuteNonQuery(_sqlCon, CommandType.StoredProcedure, DBSchemas.AddComment, sqlParametters);

            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }

        public DataSet SelectCommentByPostId(int postId)
        {
            try
            {
                var sqlParametters = new SqlParameter[1];
                sqlParametters[0] = new SqlParameter("@PostId", DbType.String) { Value = postId };
                _sqlCon = DBConnection.SqlGetConnection();
                _dataSet = SqlHelper.ExecuteDataSet(_sqlCon, CommandType.StoredProcedure, DBSchemas.GetCommentByPostId, sqlParametters);
                return _dataSet;

            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }

        public DataSet SelectCommentById(int commentId)
        {
            try
            {
                var sqlParametters = new SqlParameter[1];
                sqlParametters[0] = new SqlParameter("@CommentId", DbType.String) { Value = commentId };
                _sqlCon = DBConnection.SqlGetConnection();
                _dataSet = SqlHelper.ExecuteDataSet(_sqlCon, CommandType.StoredProcedure, DBSchemas.GetCommentById, sqlParametters);
                return _dataSet;

            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }

        public DataSet SelectCommentFromDate(DateTime fromDate)
        {
            try
            {
                var sqlParametters = new SqlParameter[1];
                sqlParametters[0] = new SqlParameter("@CommentDate", DbType.String) { Value = fromDate };
                _sqlCon = DBConnection.SqlGetConnection();
                _dataSet = SqlHelper.ExecuteDataSet(_sqlCon, CommandType.StoredProcedure, DBSchemas.GetCommentFromDate, sqlParametters);
                return _dataSet;

            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }

        public DataSet SelectAllComment()
        {
            try
            {
                _sqlCon = DBConnection.SqlGetConnection();
                _dataSet = SqlHelper.ExecuteDataSet(_sqlCon, CommandType.StoredProcedure, DBSchemas.GetAllComment);
                return _dataSet;

            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }

        public int AdminPublishComment(int commentId,string commentNote)
        {
            try
            {
                var sqlParametters = new SqlParameter[2];
                sqlParametters[0] = new SqlParameter("@CommentId", DbType.String) { Value = commentId };
                sqlParametters[0] = new SqlParameter("@CommentNote", DbType.String) { Value = commentNote };
                _sqlCon = DBConnection.SqlGetConnection();
                return SqlHelper.ExecuteNonQuery(_sqlCon, CommandType.StoredProcedure, DBSchemas.AdminPublishComment, sqlParametters);
            }
            catch (SqlException sqlException)
            {
                Logger.Error(sqlException.Message);
                throw;
            }
        }
    }
}
